﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using PsychveyApp1.classes;
using System.IO;
using System.Windows.Forms;
using System.Data;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;

namespace PsychveyApp1
{
    public partial class AnalyzeSurvey : System.Web.UI.Page
    {
        static UserAccount ua = new UserAccount();
        static Survey currentSurvey = new Survey();
        static ArrayList answerlist = new ArrayList();


        protected void Page_Load(object sender, EventArgs e)
        {
            lblUsername.Text = (string)(Session["username"]);

            //ua = (UserAccount)Session["currentUser"];
            currentSurvey = (Survey)Session["currentSurvey"];
            answerlist = DBManager.GetUserAnswersBySurveyID(currentSurvey.surveyID);
            dgvMyAnswer.DataSource = answerlist;
            dgvMyAnswer.DataBind();
            dgvMyAnswer.Visible = true;



        }

        protected void btnProceed_Click(object sender, EventArgs e)
        {
            if (ddlUser.SelectedIndex == 1)
            {

                Response.Redirect("UserAccountHome.aspx");
            }
            else if (ddlUser.SelectedIndex == 2)
            {
                Response.Redirect("Home.aspx");
            }
        }

        protected void AdminLoginShortcut_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminLogin.aspx");
        }

        protected void MySurveyLinkButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("MySurvey.aspx");
        }

        protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
        {


        }


        //public override void VerifyRenderingInServerForm(Control control)
        //{
            //required to avoid the run time error "  
            //Control 'GridView1' of type 'Grid View' must be placed inside a form tag with runat=server."  
        //}  

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Filter = "Excel files (*.xls)|*.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = true;


            Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
            try
            {

                // creating new WorkBook within Excel application
                Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
                // creating new Excelsheet in workbook
                Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
                Microsoft.Office.Interop.Excel._Worksheet worksheet1 = null;
                // see the excel sheet behind the program
                app.Visible = true;
                // get the reference of first sheet. By default its name is Sheet1.
                // store its reference to worksheet
                worksheet = workbook.Sheets["Sheet1"];
                worksheet1 = workbook.Sheets["Sheet2"];
                worksheet = workbook.ActiveSheet;
                // changing the name of active sheet
                worksheet.Name = "Answers";
                worksheet1.Name = "Legends";
                worksheet.Columns.ColumnWidth = 15;
                
                // storing header part in Excel
                for (int i = 1; i < dgvMyAnswer.Columns.Count + 1; i++)
                {

                    worksheet.Cells[1, i] = dgvMyAnswer.Columns[i - 1].HeaderText;
                    worksheet.Cells[1, i].Font.Bold = true;
                    worksheet.Cells[1, i].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                    
                }

                worksheet1.Cells[1, 2] = "1";
                worksheet1.Cells[1, 3] = "2";
                worksheet1.Cells[1, 4] = "3";
                worksheet1.Cells[1, 5] = "4";
                worksheet1.Cells[1, 6] = "5";

                worksheet1.Cells[1, 2].Font.Bold = true;
                worksheet1.Cells[1, 3].Font.Bold = true;
                worksheet1.Cells[1, 4].Font.Bold = true;
                worksheet1.Cells[1, 5].Font.Bold = true;
                worksheet1.Cells[1, 6].Font.Bold = true;

                worksheet1.Cells[1, 2].Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
                worksheet1.Cells[1, 3].Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
                worksheet1.Cells[1, 4].Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
                worksheet1.Cells[1, 5].Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
                worksheet1.Cells[1, 6].Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);

                worksheet1.Columns.HorizontalAlignment = HorizontalAlignment.Center;
               

                // storing Each row and column value to excel sheet
                for (int i = 0; i <= dgvMyAnswer.Rows.Count - 1; i++)
                {
                    for (int j = 0; j < dgvMyAnswer.Columns.Count; j++)
                    {
                        if (dgvMyAnswer.Rows[i].Cells[j].Text.Equals("Strongly Agree"))
                        {
                            worksheet.Cells[i + 2, j + 1] = "1";
                            worksheet.Columns.HorizontalAlignment = HorizontalAlignment.Center;
                        }
                        else if (dgvMyAnswer.Rows[i].Cells[j].Text.Equals("Agree"))
                        {
                            worksheet.Cells[i + 2, j + 1] = "2";
                            worksheet.Columns.HorizontalAlignment = HorizontalAlignment.Center;
                        }
                        else if (dgvMyAnswer.Rows[i].Cells[j].Text.Equals("Neutral"))
                        {
                            worksheet.Cells[i + 2, j + 1] = "3";
                            worksheet.Columns.HorizontalAlignment = HorizontalAlignment.Center;
                        }
                        else if (dgvMyAnswer.Rows[i].Cells[j].Text.Equals("Disagree"))
                        {
                            worksheet.Cells[i + 2, j + 1] = "4";
                            worksheet.Columns.HorizontalAlignment = HorizontalAlignment.Center;
                        }
                        else if (dgvMyAnswer.Rows[i].Cells[j].Text.Equals("Strongly Disagree"))
                        {
                            worksheet.Cells[i + 2, j + 1] = "5";
                            worksheet.Columns.HorizontalAlignment = HorizontalAlignment.Center;
                        }
                        else if (dgvMyAnswer.Rows[i].Cells[j].Text.Equals("True"))
                        {
                            worksheet.Cells[i + 2, j + 1] = "1";
                            worksheet.Columns.HorizontalAlignment = HorizontalAlignment.Center;
                        }
                        else if (dgvMyAnswer.Rows[i].Cells[j].Text.Equals("False"))
                        {
                            worksheet.Cells[i + 2, j + 1] = "2";
                            worksheet.Columns.HorizontalAlignment = HorizontalAlignment.Center;
                        }
                        else
                        {
                            string values = string.Empty;
                            values = dgvMyAnswer.Rows[i].Cells[j].Text;
                            worksheet.Cells[i + 2, j + 1] = values;
                            worksheet.Columns.HorizontalAlignment = HorizontalAlignment.Center;
                        }
                    }

                }

                worksheet1.Cells[2, 1] = "Likert";
                worksheet1.Cells[2, 1].Font.Bold = true;
                worksheet1.Cells[2, 2] = "S.A.";
                worksheet1.Cells[2, 3] = "A";
                worksheet1.Cells[2, 4] = "N";
                worksheet1.Cells[2, 5] = "D";
                worksheet1.Cells[2, 6] = "S.D.";

                worksheet1.Cells[3, 1] = "T&F";
                worksheet1.Cells[3, 1].Font.Bold = true;
                worksheet1.Cells[3,2] = "T"; 
                worksheet1.Cells[3,3] = "F"; 
            }
            finally
            {
                //Release the resources
                //app.Quit();
                //System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
                //app = null;

            }

 
            //----------- HTML FORMAT ------------------
            //Response.Clear();
            //Response.Buffer = true;
            //Response.ClearContent();
            //Response.ClearHeaders();
            //Response.Charset = "";
            //string FileName = "Survey_" + DateTime.Now + ".xls";
            //StringWriter strwritter = new StringWriter();
            //HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
            ////Response.Cache.SetCacheability(HttpCacheability.NoCache);
            //Response.ContentType = "application/vnd.ms-excel";
            //Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
            //dgvMyAnswer.GridLines = GridLines.Both;
            //dgvMyAnswer.RowStyle.HorizontalAlign = HorizontalAlign.Center;
            //dgvMyAnswer.HeaderStyle.Font.Bold = true;
            //dgvMyAnswer.RenderControl(htmltextwrtter);
            //Response.Write(strwritter.ToString());
            //Response.End();  

            lblDateTime.Text = "Exported time:" +  DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year + "-" + DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
            }

       
      
    }


}




